﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Easy.DAL;
using Easy.Model;

namespace Easy.BLL
{
    public class SiteMapManager
    {
        private SiteMapDAL SiteMapDal;
        public SiteMapManager()
        {
            SiteMapDal = new SiteMapDAL();
        }

        public string GetSiteMapByRoleId(Guid RoleId)
        {
            DataTable dt = SiteMapDal.GetSiteMapByRoleId(RoleId);
            string JSON = SiteMapDataToJson(dt);
            return JSON;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="UserId"></param>
        /// <param name="ClientType">客户端类型,10:电脑端，20:移动端</param>
        /// <returns></returns>
        public string GetSiteMapByUserId(Guid UserId,int ClientType)
        {
            DataTable dt = SiteMapDal.GetSiteMapByUserId(UserId, ClientType);
            string JSON = SiteMapDataToJson(dt);
            return JSON;
        }

        /// <summary>
        /// SiteMapDataTable转Json，站点图为两层结构
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        private string SiteMapDataToJson(DataTable dt)
        {
            List<SiteMapModel> list = new List<SiteMapModel>();
            foreach (DataRow item in dt.Rows)
            {
                string isparent = item["Parent"].ToString();
                if (isparent == "")
                { 
                    SiteMapModel sitemap = new SiteMapModel();
                    List<SiteMapModel> childlist = new List<SiteMapModel>();
                    sitemap.SiteMapId = item["SiteMapId"].ToString();
                    sitemap.Name = item["Name"].ToString();
                    sitemap.Url = item["Url"].ToString();
                    sitemap.Icon = item["Icon"].ToString();
                    sitemap.Privilege = item["Privilege"].ToString();
                    sitemap.ClientType = item["ClientType"].ToString();
                    if (sitemap.Icon == "")
                    {
                        sitemap.Icon = "glyphicon-list";
                    }
                    sitemap.ChildSiteMap = new List<SiteMapModel>();

                    foreach (DataRow i in dt.Rows)
                    {
                        if (sitemap.SiteMapId == i["Parent"].ToString())
                        {
                            SiteMapModel childsitemap = new SiteMapModel();
                            childsitemap.SiteMapId = i["SiteMapId"].ToString();
                            childsitemap.Name = i["Name"].ToString();
                            childsitemap.Url = i["Url"].ToString();
                            childsitemap.Icon = i["Icon"].ToString();
                            childsitemap.Privilege = i["Privilege"].ToString();
                            childsitemap.ClientType = i["ClientType"].ToString();
                            if (childsitemap.Icon == "")
                            {
                                childsitemap.Icon = "glyphicon-chevron-right";
                            }
                            sitemap.ChildSiteMap.Add(childsitemap);
                        }
                    }
                    list.Add(sitemap);
                }
            }
            return JsonConvert.SerializeObject(list);
        }
    }
}
